package com.pclong.sql;

//import org.leenlegend.sql.generator.MySQLGenerator;
//import org.leenlegend.sql.generator.OracleGenerator;
//import org.leenlegend.sql.generator.SQLServerGenerator;
import com.pclong.util.debug.ConsoleDebug;
import com.pclong.util.debug.Debug;
import com.pclong.util.debug.Log4jDebug;
import com.pclong.util.debug.SqlLiteDebug;

public class DataSourceProvider {
	
	private static String PROPERTIESFILE = "datasource.properties";
	
	private static String DATABASETYPE = "sqlite";
	private static String DRIVERCLASS = "org.sqlite.JDBC";
	private static String DATABASEURL = "jdbc:sqlite:SQLiteDB\\test.db";
	private static String USEJNDI = "false";
	private static String DATABASEJNDI = "test";
	private static String DEBUGTYPE = "console";
	
	private static boolean useJNDIBoolean = false;
	private static SQLGenerator generator;
	
	static {
		DATABASETYPE = PropertiesHelper.getProperty(PROPERTIESFILE, "databasetype").toLowerCase();
		DRIVERCLASS = PropertiesHelper.getProperty(PROPERTIESFILE, "driverclass");
		DATABASEURL = PropertiesHelper.getProperty(PROPERTIESFILE, "databaseurl");
		USEJNDI = PropertiesHelper.getProperty(PROPERTIESFILE, "usejndi").toLowerCase();
		if(USEJNDI.equals("true") || USEJNDI.equals("1") || USEJNDI.equals("yes")) 
			useJNDIBoolean = true;
		else useJNDIBoolean = false;
		if(useJNDIBoolean) DATABASEJNDI = PropertiesHelper.getProperty(PROPERTIESFILE, "databasejndi");
		DEBUGTYPE = PropertiesHelper.getProperty(PROPERTIESFILE, "debug").toLowerCase();
		//initDebug(DEBUGTYPE);
		initGenerator(DATABASETYPE);
	}

	public static String getDatabaseType() {
		return DATABASETYPE;
	}	
	public static String getDriverClass() {
		return DRIVERCLASS;
	}
	public static String getDatabaseUrl() {
		return DATABASEURL;
	}
	public static String getDatabaseJNDI() {
		return DATABASEJNDI;
	}
	public static boolean isUseJNDI() {
		return useJNDIBoolean;
	}

	public static <T> Debug getDebug(Class<T> clazz) {
		if(DEBUGTYPE.equals("log4j")) return new Log4jDebug<T>(clazz);
                if(DEBUGTYPE.equals("sqllitedebug")) return new SqlLiteDebug<T>(clazz);
		return new ConsoleDebug<T>(clazz);
	}
	public static SQLGenerator getGenerator() {
		return generator;
	}
		
	public static void initGenerator(String databaseType) {
		if(DATABASETYPE.equals("sqlite")) generator = new SQLiteGenerator();
//		else if(DATABASETYPE.equals("mysql")) generator = new MySQLGenerator();
//		else if(DATABASETYPE.equals("sqlserver")) generator = new SQLServerGenerator();
//		else if(DATABASETYPE.equals("oracle")) generator = new OracleGenerator();
		else ;
	}
	
	public static SQLGenerator getSQLGenerator() {
		return generator;
	}
	
}
